<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: gv_queue.php 54 2013-09-21 11:57:46Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 54 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:57:46 +0000 (Sat, 21 Sep 2013) $
*
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: gv_queue.php 54 2013-09-21 11:57:46Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 54 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:57:46 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.php_mail.php');

$smarty = new Smarty;

require(DIR_WS_CLASSES.'currencies.php');
$currencies = new currencies();

if ($_GET['action']=='confirmrelease' && isSET($_GET['gid'])) {
	$gv_result = $db->db_query("SELECT 
									release_flag 
								FROM 
									".TABLE_COUPON_GV_QUEUE." 
								WHERE 
									unique_id='".$_GET['gid']."'");

	if ($gv_result->fields['release_flag']=='N') {
		$gv_resulta = $db->db_query("	SELECT 
											customer_id, 
											amount 
										FROM 
											".TABLE_COUPON_GV_QUEUE ." 
										WHERE 
											unique_id='".$_GET['gid']."'");
											
		if ($gv_resulta->_numOfRows) {
			$gv_amount = $gv_resulta->fields['amount'];

			$mail = $db->db_query("	SELECT 
										customers_firstname, 
										customers_lastname, 
										customers_email_address 
									FROM 
										".TABLE_CUSTOMERS." 
									WHERE
										customers_id = '".$gv_resulta->fields['customer_id']."'");

			$smarty->assign('language', $_SESSION['language']);
			$smarty->caching = false;

			$smarty->template_dir = DIR_FS_CATALOG.'templates';
			$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';
			$smarty->config_dir = DIR_FS_CATALOG.'lang';

			$smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');
			$smarty->assign('logo_path', HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES);

			$smarty->assign('AMMOUNT',$currencies->format($gv_amount));

			$html_mail=$smarty->fetch(CURRENT_TEMPLATE.'/admin/mail/'.$_SESSION['language'].'/gift_accepted.html');
			$txt_mail=$smarty->fetch(CURRENT_TEMPLATE.'/admin/mail/'.$_SESSION['language'].'/gift_accepted.txt');

			php_mail(EMAIL_BILLING_ADDRESS,
					EMAIL_BILLING_NAME,
					$mail->fields['customers_email_address'] , 
					$mail->fields['customers_firstname'].' '.$mail->fields['customers_lastname'] , 
					'', 
					EMAIL_BILLING_REPLY_ADDRESS, 
					EMAIL_BILLING_REPLY_ADDRESS_NAME, 
					'', 
					'', 
					EMAIL_BILLING_SUBJECT, 
					$html_mail , 
					$txt_mail);

			$gv_amount = $gv_resulta->fields['amount'];

			$gv_result = $db->db_query("SELECT 
											amount 
										FROM 
											".TABLE_COUPON_GV_CUSTOMER." 
										WHERE 
											customer_id='".$gv_resulta->fields['customer_id']."'");
			$customer_gv = false;
			$total_gv_amount=0;

			if ($gv_result->_numOfRows) {
				$total_gv_amount = $gv_result->fields['amount'];
				$customer_gv = true;
			}

			$total_gv_amount = $total_gv_amount + $gv_amount;

			if ($customer_gv) {
				$gv_UPDATE=$db->db_query("	UPDATE 
												".TABLE_COUPON_GV_CUSTOMER." 
											SET 
												amount='".$total_gv_amount."' 
											WHERE 
												customer_id='".$gv_resulta->fields['customer_id']."'");
			} else {
				$gv_insert = $db->db_query("INSERT INTO 
												".TABLE_COUPON_GV_CUSTOMER."(
													customer_id, 
													amount
												)VALUES(
													'".$gv_resulta->fields['customer_id']."',
													'".$total_gv_amount."')");
			}

			$gv_update = $db->db_query("UPDATE 
											".TABLE_COUPON_GV_QUEUE." 
										SET 
											release_flag='Y' 
										WHERE 
											unique_id='".$_GET['gid']."'");
		}
	}
}

require(DIR_WS_INCLUDES.'metatag.php'); ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable">
		<tr>
			<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
					<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
			</td>
			<td class="boxCenter" valign="top">
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td width="100%">
							<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td class="pageHeading">
										<?php echo HEADING_TITLE; ?>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table border="0" width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td valign="top">
										<table border="0" width="100%" cellspacing="0" cellpadding="2" class="dataTable">
											<tr class="dataTableHeadingRow">
												<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CUSTOMERS; ?></td>
												<td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_ORDERS_ID; ?></td>
												<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_VOUCHER_VALUE; ?></td>
												<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_DATE_PURCHASED; ?></td>
												<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
											</tr>
											<?php
											$gv_list = $db->db_query("	SELECT 
																			c.customers_firstname, 
																			c.customers_lastname, 
																			gv.unique_id, 
																			gv.date_created, 
																			gv.amount, 
																			gv.order_id 
																		FROM 
																			".TABLE_CUSTOMERS." c, 
																			".TABLE_COUPON_GV_QUEUE." gv 
																		WHERE(
																				gv.customer_id = c.customers_id 
																			AND 
																				gv.release_flag = 'N')");
									  
											while (!$gv_list->EOF) { 
												$rows++;
									    
												if (((!$_GET['gid']) || (@$_GET['gid'] == $$gv_list->fields['unique_id'])) && (!$gInfo)) 
													$gInfo = new objectInfo($gv_list->fields);
									   
												if ((is_object($gInfo)) && ($$gv_list->fields['unique_id'] == $gInfo->unique_id)) 
													echo '<tr class="dataTableRowselected" onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\''.href_link('gv_queue.php', get_all_get_params(array('gid', 'action')).'gid='.$gInfo->unique_id.'&action=edit').'\'">'."\n";
												else {
													if ($i % 2 == 0) 
														$f = 'dataTableRow';
													else 
														$f = '';
													echo '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.$f.'\'" onclick="document.location.href=\''.href_link('gv_queue.php', get_all_get_params(array('gid', 'action')).'gid='.$$gv_list->fields['unique_id']).'\'">'."\n";
												}
											?>
											<td class="dataTableContent"><?php echo $$gv_list->fields['customers_firstname'].' '.$$gv_list->fields['customers_lastname']; ?></td>
											<td class="dataTableContent" align="center"><?php echo $$gv_list->fields['order_id']; ?></td>
											<td class="dataTableContent" align="right"><?php echo $currencies->format($$gv_list->fields['amount']); ?></td>
											<td class="dataTableContent" align="right"><?php echo datetime_short($$gv_list->fields['date_created']); ?></td>
											<td class="dataTableContent" align="right">
											<?php
												if ((is_object($gInfo)) && ($$gv_list->fields['unique_id'] == $gInfo->unique_id)) 
													echo image(DIR_WS_IMAGES.'icon_arrow_right.gif');
												else 
													echo '<a href="'.href_link(FILENAME_GV_QUEUE, 'page='.$_GET['page'].'&gid='.$$gv_list->fields['unique_id']).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>'; 
											?>&nbsp;
										</td>
										</tr>
										<?php 
											$gv_list->MoveNext();
										} 
										?>
										<tr>
											<td colspan="5">
												<table border="0" width="100%" cellspacing="0" cellpadding="2">
													<tr>
														<td class="smallText" valign="top"></td>
														<td class="smallText" align="right"></td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
								<?php
									$heading = array();
									$contents = array();
									switch ($_GET['action']) {
										case 'release':
											$heading[] = array('text' => '['.$gInfo->unique_id.'] '.datetime_short($gInfo->date_created).' '.$currencies->format($gInfo->amount));

											$contents[] = array('align' => 'center', 'text' => '<a class="button" style="font-color: red;" onClick="this.blur();" href="'.href_link('gv_queue.php','action=confirmrelease&gid='.$gInfo->unique_id,'NONSSL').'">'. BUTTON_CONFIRM.'</a> <a class="button" onClick="this.blur();" href="'.href_link('gv_queue.php','action=cancel&gid='.$gInfo->unique_id,'NONSSL').'">'.BUTTON_CANCEL.'</a>');
										break;
										default:
											$heading[] = array('text' => '['.$gInfo->unique_id.'] '.datetime_short($gInfo->date_created).' '.$currencies->format($gInfo->amount));

											$contents[] = array('align' => 'center','text' => '<a class="button" onClick="this.blur();" href="'.href_link('gv_queue.php','action=release&gid='.$gInfo->unique_id,'NONSSL'). '">'.BUTTON_RELEASE.'</a>');
										break;
									}

									if ( (not_null($heading)) && (not_null($contents)) ) {
										echo '<td width="25%" valign="top" class="border">'."\n";

										$box = new box;
										echo $box->infoBox($heading, $contents);

										echo '</td>'."\n";
									}
								?>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>